import { queryInstDeatilInfo, queryInstOrderInfo } from '../../services/inspect-project'
import { queryOrderById } from '../../services/order'
import { queryVehicleDetail } from '../../services/vehicle'
import { getFileJson } from '../../services/report'
import dayjs from 'dayjs'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    instId:"",
    inspectList:[],
    inspectItem:{},
    token:"",
    basicInfo:{},
    reportInfo:{},
    basicList:[
      {
        label:"充电桩",
        value:""
      },
      {
        label:"充电场站",
        value:""
      },
      {
        label:"充电开始时间",
        value:""
      },
      {
        label:"充电结束时间",
        value:""
      },
      {
        label:"充电时长",
        value:""
      },
      {
        label:"开始SOC",
        value:""
      },
      {
        label:"结束SOC",
        value:""
      },
      {
        label:"充入电能",
        value:""
      },
      {
        label:"环境温度",
        value:""
      },
      {
        label:"相对湿度",
        value:""
      },
      {
        label:"大气压力",
        value:""
      }
    ],
    batteryBasicList:[
      {
        label:"电池生厂商",
        value:""
      },
      {
        label:"电池类型",
        value:""
      },
      {
        label:"额定容量",
        value:""
      },
      {
        label:"额定电压",
        value:""
      },
      {
        label:"电池生产日期",
        value:""
      },
      {
        label:"标称能量",
        value:""
      },
      {
        label:"最高允许充电总电压",
        value:""
      },
      {
        label:"单体电池最高允许充电电压",
        value:""
      },
      {
        label:"最高允许温度",
        value:""
      },
      {
        label:"最高允许充电电流",
        value:""
      },
    ],
    keyDataList:[
      {
        title:"告警",
        imageUrl:"/assets/report-charging/warning-big.png",
        status:'',
        infoList:[
          {
            label:'告警信息',
            value: ''
          }
        ]
      },
      {
        title:"极值温度",
        imageUrl:"/assets/report-charging/temp.png",
        status:'',
        infoList:[
          {
            label:'温度差',
            value: ''
          },
          {
            label:'单体最高温度',
            value: ''
          },
          {
            label:'单体最低温度',
            value: ''
          },
        ]
      },
      {
        title:"充电速度",
        imageUrl:"/assets/report-charging/charging-speed.png",
        status:'',
        infoList:[
          {
            label:'充电速度',
            value: ''
          },
        ]
      },
      {
        title:"极值电压",
        imageUrl:"/assets/report-charging/voltage.png",
        status:'',
        infoList:[
          {
            label:'电压差',
            value: ''
          },
          {
            label:'单体最高电压',
            value: ''
          },
          {
            label:'单体最低电压',
            value: ''
          },
        ]
      },
      {
        title:"电池一致性",
        imageUrl:"/assets/report-charging/battery-consistency.png",
        status:'',
        infoList:[
          {
            label:'电压一致性',
            value: ''
          },
          {
            label:'温度一致性',
            value: ''
          },
        ]
      },
      {
        title:"SOC准确度",
        imageUrl:"/assets/report-charging/soc.png",
        status:'',
        infoList:[
          {
            label:'SOC准确度',
            value: ''
          },
          {
            label:'SOC线性度',
            value: ''
          },
        ]
      },
      {
        title:"电池健康度",
        imageUrl:"/assets/report-charging/battery.png",
        status:'',
        infoList:[
          {
            label:'电池健康度',
            value: ''
          },
        ]
      },
      {
        title:"BMS测量精度",
        imageUrl:"/assets/report-charging/inspect.png",
        status:'',
        infoList:[
          {
            label:'电压精度',
            value: ''
          },
          {
            label:'电流精度',
            value: ''
          },
        ]
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(!options){
      return
    }

    let instId = options.instId
    let orderId = options.orderId
    this.getInstDeatilInfo(instId)
    this.getOrderDetailInfo(orderId)
    this.setData({
      ['basicInfo.reportId']: instId,
      ['basicInfo.orderId']: orderId
    })
  },

  async getInstDeatilInfo(instId){
    let res = await queryInstDeatilInfo(instId)
    if(res.code === 200){
      let inspectItem = res.data
      if(inspectItem.url){
        this.handleGetFile(inspectItem.url)
      }
      this.setData({
        inspectItem,
        ['basicInfo.reportDate']: inspectItem.updateTime,
        ['basicInfo.detectStartTime']: inspectItem.detectStartTime || "",
        ['basicInfo.detectEndTime']: inspectItem.detectEndTime || "",
      })
    }
  },

  async handleGetFile(url){
    let reportInfo = await getFileJson(url)
    console.log("reportInfo>>>", reportInfo)
    this.setBasicInfo(reportInfo)
    this.setBatteryBasicList(reportInfo)
    this.setKeyData(reportInfo)
    this.setData({
      reportInfo,
      ['basicInfo.score']: reportInfo.score,
    })
  },

  setBasicInfo(reportInfo){
    let basicList = [
      {
        label:"充电桩",
        value: reportInfo.chargingPile || ""
      },
      {
        label:"充电场站",
        value: reportInfo.chargingStation || ""
      },
      {
        label:"充电开始时间",
        value: reportInfo.startTime ? dayjs(reportInfo.startTime).format('YYYY-MM-DD HH:mm:ss') : ""
      },
      {
        label:"充电结束时间",
        value: reportInfo.endTime ? dayjs(reportInfo.endTime).format('YYYY-MM-DD HH:mm:ss') : ""
      },
      {
        label:"充电时长",
        value: reportInfo.duration ?  reportInfo.duration : "",
        unit:'分钟' 
      },
      {
        label:"开始SOC",
        value: reportInfo.startSoc || "",
        unit:'%'
      },
      {
        label:"结束SOC",
        value: reportInfo.endSoc || "",
        unit:'%'
      },
      {
        label:"充入电能",
        value: reportInfo.degree ? parseFloat(reportInfo.degree).toFixed(2) : '',
        unit: 'kWh'
      },
      {
        label:"环境温度",
        value:"20",
        unit:'℃'
      },
      {
        label:"相对湿度",
        value:"34"
      },
      {
        label:"大气压力",
        value:"100",
        unit:'kpa'
      }
    ]
    this.setData({
      basicList
    })
  },

  setBatteryBasicList(reportInfo){
    let batteryBasicList = [
      {
        label:"电池生厂商",
        value: reportInfo.manufacture || ""
      },
      {
        label:"电池类型",
        value: reportInfo.batterytype || ""
      },
      {
        label:"额定容量",
        value: reportInfo.ratedcapacity ? reportInfo.ratedcapacity : '',
        unit: 'Ah'
      },
      {
        label:"额定电压",
        value: reportInfo.ratedVolt || "",
        unit: "V"
      },
      {
        label:"电池生产日期",
        value: reportInfo.productionDate || ""
      },
      {
        label:"标称能量",
        value: reportInfo.ratedEnergy || "",
        unit:'kWh'
      },
      {
        label:"最高允许充电总电压",
        value: reportInfo.highestVoltage || "",
        unit:'V'
      },
      {
        label:"单体电池最高允许充电电压",
        value: reportInfo.singlehighestVoltage || "",
        unit:"V"
      },
      {
        label:"最高允许温度",
        value: reportInfo.highestTemperature || "",
        unit:"℃"
      },
      {
        label:"最高允许充电电流",
        value: reportInfo.highestCurrent || "",
        unit: "A"
      }
    ]

    this.setData({
      batteryBasicList
    })
  },

  setKeyData(reportInfo){
    let keyDataList = [
      {
        title:"告警",
        imageUrl:"/assets/report-charging/warning-big.png",
        status: reportInfo.alarmResult,
        infoList:[
          {
            label:'告警信息',
            value: reportInfo.alarmNum,
            unit:'条'
          }
        ]
      },
      {
        title:"极值温度",
        imageUrl:"/assets/report-charging/temp.png",
        status:reportInfo.extremumTmpResult,
        infoList:[
          {
            label:'温度差',
            value: reportInfo.tmpDiff || '',
            unit: '℃'
          },
          {
            label:'单体最高温度',
            value: reportInfo.cellMaxTmp || '',
            unit: '℃'
          },
          {
            label:'单体最低温度',
            value: reportInfo.cellMinTmp || '',
            unit: '℃'
          },
        ]
      },
      {
        title:"充电速度",
        imageUrl:"/assets/report-charging/charging-speed.png",
        status:reportInfo.chargingSpeedResult,
        infoList:[
          {
            label:'充电速度',
            value: reportInfo.chargingSpeed || "",
            unit: 'kW'
          },
        ]
      },
      {
        title:"极值电压",
        imageUrl:"/assets/report-charging/voltage.png",
        status:reportInfo.extremumVResult,
        infoList:[
          {
            label:'电压差',
            value: reportInfo.vDiff ? parseFloat(reportInfo.vDiff).toFixed(2) : 0,
            unit: 'V'
          },
          {
            label:'单体最高电压',
            value: reportInfo.cellMaxV || '',
            unit: 'V'
          },
          {
            label:'单体最低电压',
            value: reportInfo.cellMinV || '',
            unit: 'V'
          },
        ]
      },
      {
        title:"电池一致性",
        imageUrl:"/assets/report-charging/battery-consistency.png",
        status: reportInfo.uniformityResult,
        infoList:[
          {
            label:'电压一致性',
            value: reportInfo.uniformityV || '',
            unit: '分'
          },
          {
            label:'温度一致性',
            value: reportInfo.tmpDiff || '',
            unit: '分'
          },
        ]
      },
      {
        title:"SOC准确度",
        imageUrl:"/assets/report-charging/soc.png",
        status:reportInfo.socResult,
        infoList:[
          {
            label:'SOC准确度',
            value: reportInfo.socAccuracy ? parseFloat(reportInfo.socAccuracy).toFixed(2):'',
            unit: '%'
          },
          {
            label:'SOC线性度',
            value: reportInfo.socLinearity || "",
            unit: '%'
          },
        ]
      },
      {
        title:"电池健康度",
        imageUrl:"/assets/report-charging/battery.png",
        status:reportInfo.sohResult,
        infoList:[
          {
            label:'电池健康度',
            value: reportInfo.sohValue || "",
            unit: '%'
          },
        ]
      },
      {
        title:"BMS测量精度",
        imageUrl:"/assets/report-charging/inspect.png",
        status:reportInfo.bmsResult,
        infoList:[
          {
            label:'电压精度',
            value: reportInfo.vAccuracy || "",
            unit: '%' 
          },
          {
            label:'电流精度',
            value: reportInfo.aAccuracy || "",
            unit: '%'
          },
        ]
      },
    ]
    this.setData({
      keyDataList
    })

  },


  async getOrderDetailInfo(orderId){
    let orderRes = await queryOrderById(orderId)
    console.log("orderRes>>>>", orderRes)
    if(orderRes.code === 200){
      let orderInfo = orderRes.data
      let { vehicleId, plateNo } = orderInfo
      this.setData({
        orderInfo,
        ['basicInfo.plateNo']: plateNo || ""
      })
      if(vehicleId){
        this.getVehicleDetailInfo(vehicleId)
      }
    }
  },

  async getVehicleDetailInfo(vehicleId){
    let res = await queryVehicleDetail(vehicleId)
    if(res.code === 200){
      let vehicleInfo = res.data
      this.setData({
        vehicleInfo,
        ['basicInfo.vin']: vehicleInfo.vin || ""
      })
    }
  }
  

})